package cn.sumpu.app.guoguojie.dao.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.guoguojie.dao.TestDao;
import cn.sumpu.app.guoguojie.domain.Test;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class TestDaoImpl extends SqlSessionDaoSupport implements TestDao {

	private static Logger logger = LoggerFactory.getLogger(TestDaoImpl.class);

	public void insert(Test test) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.TestMapper.insert",
					test);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	public void delete(Test test) throws AppBusinessException {
		try {
			getSqlSession().delete(
					"cn.sumpu.app.guoguojie.persistence.TestMapper.delete",
					test);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	public void update(Test test) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.TestMapper.update",
					test);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	public Test query(Test test) throws AppBusinessException {
		try {
			return (Test) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.TestMapper.select",
					test);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}

}
